/*  SSJumpMapPanelController.m
 *  MESLib
 * 
 * Copyright 2003-2009 Starline Software Ltd
 * 
 * This file is part of MESLib.
 * 
 * MESLib is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * MESLib is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU Lesser General Public License for more details.
 * 
 * You should have received a copy of the GNU Lesser General Public License
 * along with MESLib. If not, see <http://www.gnu.org/licenses/>.
 */
//  Created by Tobias Haughton on Sat Jun 07 2003.
//

#import "SSJumpMapPanelController.h"
#import "SSCatalystDocument.h"

@implementation SSJumpMapPanelController

+ (id)sharedJumpMapPanelController {
   static SSJumpMapPanelController *sharedJumpMapPanelController = nil;

   if(!sharedJumpMapPanelController) {
      sharedJumpMapPanelController = [[SSJumpMapPanelController allocWithZone:[self zone]] init];
   }
   return sharedJumpMapPanelController;
}

- (id)init {
   self = [super initWithWindowNibName:@"JumpMapPanel"];
   if (self) {
      [self setWindowFrameAutosaveName:@"JumpMapPanel"];
   }
   return self;
}

- (void)awakeFromNib
{
   //get the jump map from thte current document set its origin to 0,0 and display
   if([[NSDocumentController sharedDocumentController] currentDocument]) {
      SSJumpMapView *jumpMap = [[[NSDocumentController sharedDocumentController] currentDocument] jumpMapView];
      [jumpMap setFrameOrigin:NSMakePoint(0, 0)];
      [contentBox setContentView:jumpMap];
      [jumpMap setRatios];
      [jumpMap setNavRecOrigin];
   }
}

- (void)setMainWindow:(NSWindow *)mainWindow {
   NSWindowController *controller = [mainWindow windowController];
   if(controller) {
      //get the jump map from thte current document set its origin to 0,0 and display
      SSJumpMapView *jumpMap = [[[NSDocumentController sharedDocumentController] currentDocument] jumpMapView];
      [jumpMap setFrameOrigin:NSMakePoint(0, 0)];
      [contentBox setContentView:jumpMap];
      [jumpMap setRatios];
      [jumpMap setNavRecOrigin];
   } else {
      [contentBox setContentView:nil];
   }

   needsUpdate = YES;
}

- (void)windowDidUpdate:(NSNotification *)notification {
   //this is called each time the window updates
   if(needsUpdate) {
      if(![[contentBox contentView] isEqualTo:[[[NSDocumentController sharedDocumentController] currentDocument] jumpMapView]]) {
         SSJumpMapView *jumpMap = [[[NSDocumentController sharedDocumentController] currentDocument] jumpMapView];
         [jumpMap setFrameOrigin:NSMakePoint(0, 0)];
         [contentBox setContentView:jumpMap];
         [jumpMap setRatios];
         [jumpMap setNavRecOrigin];
      }
      needsUpdate = NO;
   }
}

- (IBAction)showWindow:(id)sender
{
   if([[NSDocumentController sharedDocumentController] currentDocument]) {
      //if there is a document open
      SSJumpMapView *jumpMap = [[[NSDocumentController sharedDocumentController] currentDocument] jumpMapView];
      [jumpMap setFrameOrigin:NSMakePoint(0, 0)];
      [contentBox setContentView:jumpMap];
      [jumpMap setRatios];
      [jumpMap setNavRecOrigin];
   }
   
   //set our window to be displayed in top right of screen
   [[self window] setFrameOrigin:NSMakePoint((NSWidth([[[self window] screen] visibleFrame]) - NSWidth([[self window] frame])),
                                             (NSHeight([[[self window] screen] visibleFrame]) - NSHeight([[self window] frame])))];

   [super showWindow:sender];
}

- (void)dealloc
{
   [super dealloc];
}

@end
